<?php
$img = $_GET['img'];
$w = $_GET['w'];
$h = $_GET['h'];
  
$imgInfo = getimagesize($img);
switch ($imgInfo[2]) {
	case 1: $im = imagecreatefromgif($img); break;
	case 2: $im = imagecreatefromjpeg($img);  break;
	case 3: $im = imagecreatefrompng($img); break;
	default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
}

$max_width = $w;
$max_height = $h;
list($width, $height) = getimagesize($img);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
$nWidth = intval($ratio*$width);
$nHeight = intval($ratio*$height);

$newImg = imagecreatetruecolor($nWidth, $nHeight);

/* Check if this image is PNG or GIF, then set if Transparent*/
if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
	imagealphablending($newImg, false);
	imagesavealpha($newImg,true);
	$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
	imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

//Generate the file, and rename it to $newfilename
switch ($imgInfo[2]) {
	case 1: imagegif($newImg); break;
	case 2: imagejpeg($newImg);  break;
	case 3: imagepng($newImg); break;
	default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
}
imagedestroy($newImg);
?>